﻿@using Rebazza.Server.MessageHandlers
@using Rebazza.Utils.Constants
@using Rebazza.Utils.Enums
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_DefaultLayout.cshtml";
}

<h2>Index</h2>

<script type="text/javascript">
    var socket;
    
    function WebSocketTest() {
        if ("WebSocket" in window) {
            socket = new WebSocket("ws://localhost:" + @PortConstants.Regex + "/echo");
            
            socket.onopen = function () {
                $("#status").text("Status: connected");
            };
            
            socket.onmessage = function (evt) {
                alert(evt.data);
            };
            
            socket.onclose = function () {
                $("#status").text("Status: closed");
            };
        }
        else {
            $("#status").text("Status: WebSocket NOT supported by your Browser!");
        }
    }

    function SendMessage() {
        var json2 = {
            Type: '@InternalType.Enter',
            RoomNumber: 1
        };

        var json = {
            Mode : "@CommandMode.Base",
            Type: "@CommandType.Roomed",
            Command: json2,
            Status: "@CommandStatus.Success"
        };

        socket.send(JSON.stringify(json));
    }

    function ExitRoom(number) {
        var json2 = {
            Type: '@InternalType.Exit',
            RoomNumber: number
        };

        var json = {
            Mode: "@CommandMode.Base",
            Type: "@CommandType.Roomed",
            Command: json2,
            Status: "@CommandStatus.Success"
        };

        socket.send(JSON.stringify(json));
    };
</script>

<div id="sse">
    <a href="javascript:WebSocketTest()">Run WebSocket</a>
</div>

<div>
    <input id="message" type="text"/>

    <button onclick="SendMessage()">Enter</button>
    <button onclick="ExitRoom(1)">Exit</button>
</div>

<div id="status">
    Status: closed
</div>